import { createStore } from 'vuex'

export default createStore({
  state: {
    cartList:[]
  },
  mutations: {
    addCart(state,payload){
      // let oldProduct = null;

      // for (let item of state.cartList) {
      //   if(item.iid === payload.iid) {
      //     oldProduct = item;
      //   }
      // }

      let oldProduct = state.cartList.find(item => item.iid === payload.iid)

      if (oldProduct) {
        oldProduct.count += 1
      } else {
        payload.count = 1
        payload.checked = true;
        payload.price = payload.price.substr(1);
        state.cartList.push(payload)
      }
    }
  },
  actions: {
  },
  modules: {
  }
})
